const { list, userList } = require('./mock')
const bodyParser = require('body-parser')
const JWT = require('jsonwebtoken')
module.exports = app => {
	app.use(bodyParser.json())
	app.get('/api/list', (req, res) => {
		// 获取查询参数 
		const { pageCode = 1, pageSize = 10 } = req.query;

		if (pageCode * pageSize > list.length) {
			res.send({
				code: 400,
				message: '没有更多数据了',
			})
		} else {
			res.send({
				code: 200,
				message: '数据获取成功',
				data: list.slice((pageCode - 1) * pageSize, pageCode * pageSize)
			})
		}
	})


	app.post('/api/login', (req, res) => {

		const { tel, code } = req.body;
		const item = userList.find(v => v.tel === tel && code)

		if (item) {
			res.send({
				code: 200,
				data: {
					...item,
					token: JWT.sign(item, '666666')
				},
				message: '登录成功',
			})
		} else {
			res.send({
				code: 403,
				message: '手机号或验证码错误'
			})
		}

	})
}